package org.jeecg.modules.device.com.tianhang.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @ClassName GaoDeMapUtil
 * @Description: TODO
 * @Author 86152
 * @Date 2023/9/5
 * @Version V1.0
 **/
@Slf4j
@Component
public class GaoDeMapUtil {

    private static final String GAO_DE_KEY = "801448098fe8ee91f4f02cbf2b768e7b";

    public Object getLonLat(String address) {
        try {
            // 返回输入地址address的经纬度信息, 格式是 经度,纬度
            String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key=" + GAO_DE_KEY + "&address=" + address;
            // 高德接口返回的是JSON格式的字符串
            String queryResult = getResponse(queryUrl);
            JSONObject job = JSONObject.parseObject(queryResult);
            JSONObject jobJSON = JSONObject
                    .parseObject(
                            job.get("geocodes").toString().substring(1, job.get("geocodes").toString().length() - 1));
            String LngAndLat = jobJSON.get("location").toString();
            log.info("经纬度为：" + LngAndLat);
            return LngAndLat;
        } catch (Exception e) {
            return e.toString();
        }
    }


    /**
     * 功能描述: 根据两个定位点的经纬度算出两点间的距离
     *
     * @param startLonLat 起始经纬度
     * @param endLonLat   结束经纬度（目标经纬度）
     * @return java.lang.Long 两个定位点之间的距离
     * @author
     * @date
     */
    public Object getDistance(String startLonLat, String endLonLat) {
        try {
            // 返回起始地startAddr与目的地endAddr之间的距离，单位：米
            Long result = new Long(0);
            String queryUrl =
                    "http://restapi.amap.com/v3/distance?key=" + GAO_DE_KEY + "&origins=" + startLonLat
                            + "&destination="
                            + endLonLat;
            String queryResult = getResponse(queryUrl);
            JSONObject job = JSONObject.parseObject(queryResult);
            JSONArray ja = job.getJSONArray("results");
            JSONObject jobO = JSONObject.parseObject(ja.getString(0));
            result = Long.parseLong(jobO.get("distance").toString());
            // log.info("距离：" + result);
            return result;
        } catch (Exception e) {
            return e.toString();
        }
    }

        /**
         * 功能描述: 发送请求
         *
         * @param serverUrl 请求地址
         * @return java.lang.String
         * @author
         * @date
         */
        private static String getResponse (String serverUrl){
            // 用JAVA发起http请求，并返回json格式的结果
            StringBuffer result = new StringBuffer();
            try {
                URL url = new URL(serverUrl);
                URLConnection conn = url.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
                in.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result.toString();
        }



}
